import random

from map.base import Map
from alg.astar import Astar
from alg.dfs import Dfs
from map.randmap import RandMap
from utils.utils import idx2rgba

# Test
if __name__ == '__main__':

    # 随机地图生成
    basemap = RandMap(1, 255, walls=50).rand()

    # ==================DFS=================
    dfs_algo = Dfs(basemap)
    dfs_roads, dfs_steps, dfs_sum_weights = dfs_algo.solve()

    if dfs_roads is None:
        print(f"No road found")
        exit()

    # 输出相关数值
    print(f"The total weight is: {dfs_sum_weights}")
    print(f"The total steps is: {len(dfs_steps)}")
    # =====================AStar=====================
    astar_algo = Astar(basemap)
    astar_roads, astar_steps, astar_sum_weights = astar_algo.solve()

    if astar_roads is None:
        print(f"No road found")
        exit()

    # 输出相关数值
    print(f"The total weight is: {astar_sum_weights}")
    print(f"The total steps is: {len(astar_steps)}")

    # ---------------------------
    if astar_sum_weights != dfs_sum_weights:
        print(f"The tow method no same")
        exit()
    # ---------------------------

    # DFS绘制结果地图
    dfs_total_map = Map(basemap)

    # # 绘制搜索过程
    dfs_total_map.add_rects(dfs_steps)
    dfs_total_map.plot_map(showeight=True)
    dfs_total_map.plot_rects()
    dfs_total_map.clear_rects()

    # # 绘制路径
    dfs_total_map.add_rects(dfs_roads)
    dfs_total_map.plot_rects(rsc=(255, 0, 0), rec=(255, 0, 0))

    dfs_total_map.save(f"dfs.png", dpi=200)

    # AStar绘制结果地图
    astar_total_map = Map(basemap)

    # # 绘制搜索过程
    astar_total_map.add_rects(astar_steps)
    astar_total_map.plot_map(showeight=True)
    astar_total_map.plot_rects()
    astar_total_map.clear_rects()

    # # 绘制路径
    astar_total_map.add_rects(astar_roads)
    astar_total_map.plot_rects(rsc=(255, 0, 0), rec=(255, 0, 0))

    astar_total_map.save(f"astar.png", dpi=200)

    # DFS绘制演示过程
    for idx in range(len(dfs_steps) + len(dfs_roads)):
        dfs_total_map = Map(basemap)
        dfs_total_map.plot_map(showeight=True)

        if idx < len(dfs_steps):
            dfs_total_map.add_rects(dfs_steps[:idx])
            dfs_total_map.plot_rects()
        else:
            dfs_total_map.add_rects(dfs_steps)
            dfs_total_map.plot_rects()
            dfs_total_map.clear_rects()

            dfs_total_map.add_rects(dfs_roads[len(dfs_roads) - (idx - len(dfs_steps)):])
            dfs_total_map.plot_rects(rsc=(255, 0, 0), rec=(255, 0, 0))
        dfs_total_map.save(f"dfs/{idx}.png", dpi=200)
        dfs_total_map.close()

    # AStar绘制演示过程
    for idx in range(len(astar_steps) + len(astar_roads)):
        astar_total_map = Map(basemap)
        astar_total_map.plot_map(showeight=True)

        if idx < len(astar_steps):
            astar_total_map.add_rects(astar_steps[:idx])
            astar_total_map.plot_rects()
        else:
            astar_total_map.add_rects(astar_steps)
            astar_total_map.plot_rects()
            astar_total_map.clear_rects()

            astar_total_map.add_rects(astar_roads[len(astar_roads) - (idx - len(astar_steps)):])
            astar_total_map.plot_rects(rsc=(255, 0, 0), rec=(255, 0, 0))
        astar_total_map.save(f"astar/{idx}.png", dpi=200)
        astar_total_map.close()
